package com.estate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.remoting.exchange.Request;
import com.alibaba.fastjson.JSONArray;
import com.estate.pojo.Ban;
import com.estate.pojo.Detail;
import com.estate.pojo.Project;
import com.estate.pojo.Region;
import com.estate.service.building.BuildingService;
import com.estate.service.detail.DetailService;
import com.estate.service.project.ProjectService;
import com.estate.service.region.RegionService;

@Controller
public class BuildingController {

	private Logger logger = Logger.getLogger(BuildingController.class);//redirect:

	@Autowired
	private BuildingService buildingService;
	
	@Autowired
	private DetailService detailService;

	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private RegionService regionService;
	
	// 跳转路径
	@RequestMapping("/buildinginfo")
	public ModelAndView builDingPath(ModelAndView model) {
		List<Project> projectList = projectService.getAllProject();
		model.addObject("projectList", projectList);
		model.setViewName("thymeleaf/buildinginfo");
		return model;
	}

	// 根据项目id查询项目信息
	@ResponseBody
	@RequestMapping(value = "/getBuildInfoByPoid", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Object getBuildInfoByPoid(Integer ProId) {
		List<Project> porjectlist = new ArrayList<>();
		porjectlist = projectService.getProjectByProId(ProId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectlist", porjectlist);
		return JSONArray.toJSONString(map);
	}

	// 增加区域跳转路径并查询项目的分期项目编号
	@RequestMapping(value = "/addBuildRePath")
	public ModelAndView addBuildRePath(ModelAndView model, Integer ProId) {
		List<Detail> detCodingList = new ArrayList<>();
		detCodingList = detailService.findDetCodingByProId(ProId);
		model.setViewName("Thymeleaf/addBulidingRegion");
		model.addObject("detCodingList", detCodingList);
		return model;
	}

	// 增加区域
	@ResponseBody
	@RequestMapping(value = "/addBuildRe")
	public Object addBuildRe(Region region) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", regionService.addRegion(region));
		return JSONArray.toJSONString(map);
	}

	// 增加楼栋路劲并查询项目的分期项目编号
	@RequestMapping(value = "/addBuildPath")
	public ModelAndView addBuildPath(ModelAndView model, Integer ProId) {
		/*List<Region> regionCodeList = new ArrayList<>();
		regionCodeList = regionService.findCodeByProId(ProId);*/
		List<Detail> CodeList = new ArrayList<>();
		List<Region> regionCodeList = new ArrayList<>();
		CodeList = detailService.findCodeByProId(ProId);
		for (Detail detail : CodeList) {
			regionCodeList = detail.getRegionlist();
		}
		model.setViewName("Thymeleaf/addbuilding");
		model.addObject("regionCodeList", regionCodeList);
		return model;
		/*String id= request.getParameter("detId");
		Integer.valueOf(id);*/
		
	}

	// 增加楼栋
	@ResponseBody
	@RequestMapping(value = "/addBuild", method = RequestMethod.POST)
	public Object addBuild(@RequestBody Ban ban) {
		Map<String, Object> map = new HashMap<String, Object>();
		/*Integer id =ban.getDiyId();
		int RegionId = regionService.findRegionIdByDetilId(id);
		ban.setBanRegionId(RegionId);*/
		map.put("num", buildingService.addBuilding(ban));//执行添加
		return JSONArray.toJSONString(map);
	}


}
